//
//  LTFNBAContentViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/28.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFBeautyContentViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFBeautyWebContentModel.h"
#import "LTFBeautyWebModel.h"
#import "LTFBeautyWebCollectionViewCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface LTFBeautyContentViewController ()
<
 UICollectionViewDataSource,
 UICollectionViewDelegate
>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *collectionViewArray;
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UILabel *myLabel;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *numberLabel;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UIButton *button;
@end

@implementation LTFBeautyContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor blackColor];
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 24, WIDTH, 40)];
    _headView.backgroundColor = [UIColor blackColor];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [_headView addSubview:_button];
    _button.frame = CGRectMake(5, 0, 70, 40);
    [_button setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    _button.tintColor = [UIColor whiteColor];
    [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self createView];
}
- (void)click:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.view addSubview:_headView];
}
- (void)viewWillDisappear:(BOOL)animated {
    [_headView removeFromSuperview];
}
- (void)createView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH, (HEIGHT - 64) / 5 * 4 - 50);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing= 0;
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (HEIGHT - 64) / 5 * 4 - 50) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[LTFBeautyWebCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFBeautyWebCollectionViewCell class])];
    
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, (HEIGHT) / 5 * 4 , WIDTH, 100)];
    _myView.backgroundColor = [UIColor blackColor];
    _myView.alpha = 0.5;
    [self.view addSubview:_myView];
    [_myView release];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, (WIDTH - 20) / 6 * 5, 30)];
    [_myView addSubview:_titleLabel];
    _titleLabel.textColor = [UIColor whiteColor];
    [_titleLabel release];
    _titleLabel.font = [UIFont systemFontOfSize:20];
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 20) / 6 * 5 + 10, 10, (WIDTH - 20) / 6, 30)];
    _numberLabel.textColor = [UIColor whiteColor];
    [_myView addSubview:_numberLabel];
    [_numberLabel release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, WIDTH - 20, 100)];
    [_myView addSubview:_myLabel];
    [_myLabel release];
    _myLabel.textColor = [UIColor whiteColor];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [_myView addGestureRecognizer:tap];
    [tap release];
}
- (void)panAction:(UITapGestureRecognizer *)tap {
    if (self.myView.frame.origin.y == (HEIGHT- 64 - (self.myLabel.frame.size.height + 45))) {
        self.myView.frame = CGRectMake(0, (HEIGHT)/ 5 * 4 - 50, WIDTH, self.myLabel.frame.size.height + 45);
    }
        else {
    self.myView.frame = CGRectMake(0, HEIGHT- 64 - (self.myLabel.frame.size.height + 45), WIDTH, self.myLabel.frame.size.height + 45);
        }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionViewArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LTFBeautyWebContentModel *model = self.collectionViewArray[indexPath.row];
    LTFBeautyWebCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFBeautyWebCollectionViewCell class]) forIndexPath:indexPath];
    [cell.picImageView setImageWithURL:[NSURL URLWithString:model.imgurl] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    self.myLabel.text = model.note;
    self.myLabel.numberOfLines = 0;
    [self.myLabel sizeToFit];
    self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld", (indexPath.row + 1), self.collectionViewArray.count];
    self.myView.frame = CGRectMake(0, (HEIGHT)/ 5 * 4 - 50, WIDTH, self.myLabel.frame.size.height + 45);
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    LTFBeautyWebContentModel *model = self.collectionViewArray[(NSInteger)(collectionView.contentOffset.x / WIDTH)];
    self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld", (NSInteger)(collectionView.contentOffset.x / WIDTH + 1), self.collectionViewArray.count];
    self.myLabel.text = model.note;
    self.myLabel.numberOfLines = 0;
    [self.myLabel sizeToFit];
    self.myView.frame = CGRectMake(0, (HEIGHT)/ 5 * 4 - 50, WIDTH, self.myLabel.frame.size.height + 45);
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
        [self createData];
    }
}
- (void)createData {
    self.collectionViewArray = [NSMutableArray array];
    NSString *str = [NSString string];
    if (self.string.length == 5) {
        str = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/0096/%@.json", self.string];
    }
    else {
    str = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@.json", self.string];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        LTFBeautyWebModel *model = [[[LTFBeautyWebModel alloc] init] autorelease];
        [model setValuesForKeysWithDictionary:dictionary];
        
        for (NSDictionary *dic in (NSArray *)model.photos) {
            LTFBeautyWebContentModel *model = [[LTFBeautyWebContentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.collectionViewArray addObject:model];
            [model release];
        }
        self.titleLabel.text = model.setname;
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
